import { Controller } from 'egg';

class AssuageGriefController extends Controller {

    /**
     * 查询解忧列表
     */
    public async searchAssuageGriefList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchAssuageGriefList(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 根据标题查找话题列表
     */
    public async searchTopicByTitle() {
        const { ctx, ctx: { request } } = this;
        // try {
        //     ctx.validate({
        //         topic_title: 'string',
        //     }, request.body)
        // } catch (e) {
        //     ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
        //     return;
        // }
        const result = await ctx.service.assuageGriefService.searchTopicByTitle(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 屏蔽、解除屏蔽
     */
    public async showAndShieldAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                assuage_grief_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.assuageGriefService.showAndShieldAssuageGriefer(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 微信端查询全部解忧
     */
    public async searchAssuageGriefAll() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                user_id: 'string',
                school_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchAssuageGriefAll(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 微信端查询我关注的朋友的解忧
     */
    public async searchFriendsAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchFriendsAssuageGrief(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 查找我的解忧
     */
    public async searchMyAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchMyAssuageGrief(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 根据用户id查询解忧列表
     */
    public async searchAssuageGriefListById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                target_user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchAssuageGriefListById(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 删除解忧
     */
    public async deleteAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                assuage_grief_id: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.assuageGriefService.deleteAssuageGrief(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找我收藏的解忧
     */
    public async searchCollectionAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchCollectionAssuageGrief(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 处理收藏解忧（收藏、取消收藏）
     */
    public async handleCollection() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                type: 'type',
                assuage_grief_id: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.assuageGriefService.handleCollection(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 根据解忧id查找解忧详情
     */
    public async searchAssuageGriefById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                assuage_grief_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.assuageGriefService.searchAssuageGriefById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 发布解忧
     */
    public async createAssuageGrief() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                assuage_grief_anonymous: 'type',
                assuage_grief_content: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.assuageGriefService.createAssuageGrief(request.body);
        ctx.body = ctx.helper.responseSuccess('发布成功');
    }
}
export default AssuageGriefController;